const { createServer } = require("http");
const express = require("express");
const { Server } = require("socket.io");
const app = express();
app.use(express.static("./public"));
const server = createServer(app); // http server
const io = new Server(server);

app.get("/user", (req, res) => {
  res.send("hello");
});

io.on("connection", (socket) => {
  console.log("ws服务器：有人链接");

  let h = setTimeout(function fn() {
    io.emit("/a/b", `server: ${new Date().toLocaleString()}`);
    h = setTimeout(fn, Math.random() * 10000 + 5000);
  });

  socket.on("c1", (data) => {
    // console.log("ws服务器：客户端信息", data);
    io.emit("/user/live/add", data); //广播
  });
  socket.on("disconnect", () => {
    console.log("ws服务器：有人断开链接");
  });
});

server.listen(3000, () => console.log("http://127.0.0.1:3000"));
